package com.cjm.algorithm.queue;

import java.util.ArrayDeque;
import java.util.Deque;

public class RemoveDuplicates1047 {
    public String removeDuplicates(String s) {
        if (s == null || s.length() == 1) {
            return s;
        }
        Deque<Character> queue = new ArrayDeque<>();
        queue.addLast(s.charAt(0));
        Character pre = s.charAt(0);
        for (int i = 1; i < s.length(); i++) {
            Character c = s.charAt(i);
            if (c.equals(pre)) {
                queue.pollLast();
                pre = queue.peekLast();
                continue;
            }
            queue.addLast(c);
            pre = c;
        }
        StringBuilder sb = new StringBuilder();
        while (!queue.isEmpty()) {
            sb.append(queue.pollFirst());
        }
        return sb.toString();
    }
}
